package freemap;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author ChrisAD
 */
public class Node {

    private Long created;
    private String ID;
    private Long modified;
    private String text;
    private Position position;
    private List<Node> children;
    private Boolean folded;

    public Node(Long created, String ID, String text) {
        this.created = created;
        this.ID = ID;
        this.text = text;
        this.modified = created;
    }

   public Node(String text, String id) {
        this.created = System.currentTimeMillis();
        this.ID = id;
        this.text = text;
        this.modified = created;        
    }      
    
    
//    //Test
//    public Node(Long created, String text) {
//        this.created = created;
//        this.text = text;
//        this.modified = created;
//    }
//    
   
    

    public String toFreemindFormat() {
        String output = String.format("<node CREATED=\"%s\" ID=\"%s\" MODIFIED=\"%s\" TEXT=\"%s\"", this.getCreated(), this.getID(), this.getModified(), this.getText());
//        String output = String.format("\t<node CREATED=\"%s\" MODIFIED=\"%s\" TEXT=\"%s\"", this.getCreated(), this.getModified(), this.getText());
        if (this.getPosition() != null) {
            output += String.format(" POSITION=\"%s\"", this.getPosition().toString().toLowerCase());
        }
        if (this.getFolded() != null) {
            output += String.format(" FOLDED=\"%s\"", this.getFolded().toString().toLowerCase());
        }
        if (this.getChildren().isEmpty()) {
            output += "/>\r\n";
        } else {
            output += ">\r\n";
            for (Node childNode : this.getChildren()) {
                output += childNode.toFreemindFormat();
//                System.out.println(output);
            }
            output += "</node>\r\n";
        }
        return output;
    }    
    
    public String getID() {
        return ID;
    }

    public Long getCreated() {
        return created;
    }

    public Boolean getFolded() {
        return folded;
    }

    public Long getModified() {
        return modified;
    }

    public Position getPosition() {
        return position;
    }

    public void setPosition(Position position) {
        this.modified = System.currentTimeMillis();
        this.position = position;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.modified = System.currentTimeMillis();
        this.text = text;
    }

    public List<Node> getChildren() {
        if (children == null) {
            children = new ArrayList<Node>();
        }
        return children;
    }

    public void setChildren(List<Node> children) {
        this.children = children;
        this.modified = System.currentTimeMillis();
    }

    public void addChild(Node n) {
        this.getChildren().add(n);
        this.modified = System.currentTimeMillis();
    }
    
    public Boolean isFolded() {
        if (folded == null) {
            folded = false;
        }
        return folded;
    }

    public void setFolded(Boolean folded) {
        this.folded = folded;
        this.modified = System.currentTimeMillis();
    }
    
  
}
